//
//  BasicViewController.m
//  NiuBanJinGold
//
//  Created by Billy on 15/9/24.
//  Copyright (c) 2015年 zzjr. All rights reserved.
//

#import "BasicViewController.h"
#import "MyAccountViewController.h"
#import "MoreSettingsViewController.h"
#import "TicketsViewController.h"
#import "WalletViewController.h"
#import "LoginviewController.h"

@interface BasicViewController () <UIGestureRecognizerDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *view_scroll ;//支持4S/5

@end

@implementation BasicViewController

#pragma mark - VC initial method

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self){

        // 返回按钮
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,35, 35)];
        
        [backButton addTarget:self action:@selector(backToPreviousPage) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *barButton_back = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        
        UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"newBack"]];
        
        imageView.frame=CGRectMake(0, 8,14, 20);
        
        [backButton addSubview:imageView];
        
        [self.navigationItem setLeftBarButtonItem:barButton_back];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(iPhone4 || iPhone5){
        
        self.view_scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
        [self.view_scroll setShowsVerticalScrollIndicator:NO];
        [self.view_scroll setContentSize:CGSizeMake(0, SCREEN_HEIGHT)];
        self.view = self.view_scroll;
    }
    
    [self pageSetting]; //设置页面元素
}

#pragma mark - page setting

- (void) pageSetting {
    
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    // 导航栏背景颜色，要适配
    if (SYSTEM_VERSION >= 7.0) {
        
        self.navigationController.navigationBar.barTintColor = [UsefulUtil colorWithHexString:@"#ffffff"];
        self.navigationController.navigationBar.tintColor = [UsefulUtil colorWithHexString:@"#ffffff"];
    } else {
        
        self.navigationController.navigationBar.tintColor = [UsefulUtil colorWithHexString:@"#ffffff"];
    }
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:[UsefulUtil colorWithHexString:@"#000000"],
      NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:17], NSFontAttributeName, nil]];
    
    if (SYSTEM_VERSION >= 7.0) {
        
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    
//    // 左右滑动手势
//    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiperGestureHandle:)];
//    
//    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
//    
//    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiperGestureHandle:)];
//    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
//    
//    if (iPhone4) {
//        
//        [self.view_scroll addGestureRecognizer:swipeLeft];
//        [self.view_scroll addGestureRecognizer:swipeRight];
//    } else {
//        
//        [self.view addGestureRecognizer:swipeLeft];
//        [self.view addGestureRecognizer:swipeRight];
//    }
}

#pragma mark - set rightbarbuttonItem

- (void) setRightBarButtonItemTitle:(NSString *) title imageName:(NSString *) name actionName:(SEL)action {
    
    // 图片在前，文字在后
    
    UIButton *button_rightItem = [[UIButton alloc] initWithFrame:CGRectMake(MULTIPLYWIDTH(40.0/2), 0, MULTIPLYWIDTH(166.0/2), MULTIPLYHEIGHT(86.0/2))];
    
    [button_rightItem setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    
    UIImageView *imageView_icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MULTIPLYWIDTH(166.0/2), MULTIPLYHEIGHT(86.0/2))];
    
    [imageView_icon setImage:[UIImage imageNamed:name]];
    
    [button_rightItem addSubview:imageView_icon];
    
    
    [button_rightItem addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItemRight = [[UIBarButtonItem alloc] initWithCustomView:button_rightItem];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    /**
     *  width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和边界间距为5pix，所以width设为-5时，间距正好调整
     *  为0；width为正数时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacer.width = MULTIPLYWIDTH(-15.0);
    [self.navigationItem setRightBarButtonItems:@[negativeSpacer,barItemRight]];
}

- (void) action { //默认不实现任何code
    
    NSLog(@"super action");
}

#pragma mark - custom return back method

- (void) backToPreviousPage {
    
    [self.navigationController popViewControllerAnimated:YES];
}

//#pragma mark - getsture method default implementation
//
//- (void) swiperGestureHandle:(UISwipeGestureRecognizer *) swipe {
//    
//    UITabBarController *tabbar = (UITabBarController *) CURRENT_WINDOW.rootViewController ;
//    
//    NSUInteger index = tabbar.selectedIndex;
//    
//    if  (index < 3 && swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
//
//        tabbar.selectedIndex += 1;
//        
//        CATransition* animation = [CATransition animation];
//        [animation setDuration:0.2f];
//        [animation setType:kCATransitionMoveIn];
//        [animation setSubtype:kCATransitionFromRight];
//        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
//        [[tabbar.view layer]addAnimation:animation forKey:@"switchView"];
//        
//    }else if( index > 0 && swipe.direction == UISwipeGestureRecognizerDirectionRight){
//        
//        tabbar.selectedIndex -= 1;
//        CATransition* animation = [CATransition animation];
//        [animation setDuration:0.2f];
//        [animation setType:kCATransitionReveal];
//        [animation setSubtype:kCATransitionFromLeft];
//        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
//        [[tabbar.view layer]addAnimation:animation forKey:@"switchView"];
//    }
//}

#pragma mark - hide keyboard

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

//    [[[NetworkUtil sharedNetworkUtil] getManager].operationQueue cancelAllOperations]; //将来你有能力后可以开放
}

@end
